
using System;
using System.Text;
using SpotifyGenNHibernate.CEN;
using NHibernate;
using NHibernate.Cfg;
using NHibernate.Criterion;
using NHibernate.Exceptions;
using SpotifyGenNHibernate.EN.Spotify;

namespace SpotifyGenNHibernate.CAD.Spotify
{
        public partial class ArtistaCAD : BasicCAD, IArtistaCAD
        {
        public ArtistaCAD() : base ()
        {
        }

        public ArtistaCAD(ISession sessionAux) : base (sessionAux)
        {
        }



        public ArtistaEN ReadOIDDefault (int IdArtista)
        {
                ArtistaEN artistaEN = null;

                try
                {
                        SesssionInitializeTransaction ();
                        artistaEN = (ArtistaEN)session.Load (typeof(ArtistaEN),
                                IdArtista

                                );
                        SessionCommit ();
                }

                catch (Exception ex) {
                        SessionRollBack ();
                        if (ex is ModelException)
                                throw ex;
                        throw new DataLayerException ("Error in ArtistaCAD.", ex);
                }


                finally
                {
                        SessionClose ();
                }

                return artistaEN;
        }


        public int CrearArtista (ArtistaEN artista)
        {
                try
                {
                        SesssionInitializeTransaction ();
                        if (artista.Album != null) {
                                foreach (SpotifyGenNHibernate.EN.Spotify.AlbumEN item in artista.Album)
                                        session.Save (item);
                        }

                        session.Save (artista);
                        SessionCommit ();
                }

                catch (Exception ex) {
                        SessionRollBack ();
                        if (ex is ModelException)
                                throw ex;
                        throw new DataLayerException ("Error in ArtistaCAD.", ex);
                }


                finally
                {
                        SessionClose ();
                }

                return artista.IdArtista;
        }

        public ArtistaEN ObtenerDatos (int IdArtista)
        {
                ArtistaEN artistaEN = null;

                try
                {
                        SesssionInitializeTransaction ();
                        artistaEN = (ArtistaEN)session.Load (typeof(ArtistaEN),
                                IdArtista

                                );
                        SessionCommit ();
                }

                catch (Exception ex) {
                        SessionRollBack ();
                        if (ex is ModelException)
                                throw ex;
                        throw new DataLayerException ("Error in ArtistaCAD.", ex);
                }


                finally
                {
                        SessionClose ();
                }

                return artistaEN;
        }

        public void ModificarDatos (ArtistaEN artista)
        {
                try
                {
                        SesssionInitializeTransaction ();
                        ArtistaEN artistaEN = (ArtistaEN)session.Load (typeof(ArtistaEN), artista.IdArtista);

                        artistaEN.Nombre = artista.Nombre;


                        artistaEN.Descripcion = artista.Descripcion;

                        session.Update (artistaEN);
                        SessionCommit ();
                }

                catch (Exception ex) {
                        SessionRollBack ();
                        if (ex is ModelException)
                                throw ex;
                        throw new DataLayerException ("Error in ArtistaCAD.", ex);
                }


                finally
                {
                        SessionClose ();
                }
        }
        public void BorrarArtista (int IdArtista)
        {
                try
                {
                        SesssionInitializeTransaction ();
                        ArtistaEN artistaEN = (ArtistaEN)session.Load (typeof(ArtistaEN),
                                IdArtista

                                );
                        session.Delete (artistaEN);
                        SessionCommit ();
                }

                catch (Exception ex) {
                        SessionRollBack ();
                        if (ex is ModelException)
                                throw ex;
                        throw new DataLayerException ("Error in ArtistaCAD.", ex);
                }


                finally
                {
                        SessionClose ();
                }
        }
        }
}
